import java.io.*;
import java.nio.file.Files;

public class Main {
    public static void main(String[] args){
        System.out.println("执行bash命令咯");
        try {
            executeCommands();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void executeCommands() throws IOException {
        File tempScript = createTempScript();
        try {
            ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString());
            Process process = pb.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String result;
            while ((result = reader.readLine()) != null) {
                System.out.println(result);
            }
            reader.close();
        } finally {
            Files.delete(tempScript.toPath());
        }
    }

    public static File createTempScript() throws IOException {
        File tempScript = File.createTempFile("script", null);
        Writer streamWriter = new OutputStreamWriter(new FileOutputStream(tempScript));
        PrintWriter printWriter = new PrintWriter(streamWriter);
        printWriter.println("#!/bin/bash");
        printWriter.println("cd /Users/chounaidai/Documents/hyperledger/mynetwork ");
        printWriter.println("ls");
        printWriter.close();
        return tempScript;
    }
}